package com.vhall.component.service.watchlimit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.watchlimit.ApplyUsersMapper;
import com.vhall.component.dao.watchlimit.LimitApplyMapper;
import com.vhall.component.dao.watchlimit.WhiteAccountsMapper;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.room.vo.ApplyEntityDTO;
import com.vhall.component.entity.watchlimit.ApplyEntity;
import com.vhall.component.entity.watchlimit.ApplyUserEntity;
import com.vhall.component.entity.watchlimit.WhiteAccountsEntity;
import com.vhall.component.entity.watchlimit.dto.ApplyUserEntityDTO;
import com.vhall.component.entity.watchlimit.dto.WhiteAccountsEntityDTO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.constant.AccountConstant;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.room.RoomInnerService;
import com.vhall.component.service.watchlimit.WatchLimitInnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lsl
 * @since 2021/6/25
 */
@Service
public class WatchLimitInnerServiceImpl implements WatchLimitInnerService {
    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private WhiteAccountsMapper whiteAccountsMapper;

    @Autowired
    private LimitApplyMapper limitApplyMapper;

    @Autowired
    private ApplyUsersMapper applyUsersMapper;

    @Override
    public Boolean checkWhiteAccount(String roomId, String password, String phone) {
        // 观看限制检测
        RoomsEntityDTO roomsEntityDTO = roomInnerService.getRoomInfo(roomId);
        if (roomsEntityDTO == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        if (roomsEntityDTO.getLimitType().equals(AccountConstant.ACCOUNT_TYPE_VISITOR)) {
            // 白名单信息查询
            LambdaQueryWrapper<WhiteAccountsEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(WhiteAccountsEntity::getWhitename, phone);
            wrapper.and(wq -> wq.eq(WhiteAccountsEntity::getRoomId, roomsEntityDTO.getRoomId()).or().eq(WhiteAccountsEntity::getIlId, roomsEntityDTO.getIlId()));
            wrapper.last("limit 1");
            WhiteAccountsEntity whiteAccountsEntity = whiteAccountsMapper.selectOne(wrapper);
            if (whiteAccountsEntity == null || whiteAccountsEntity.getWhitename() == null) {
                throw new BusinessException(BizErrorCode.EMPTY_WHITE_USER);
            } else if (!whiteAccountsEntity.getWhitepaas().equals(password)) {
                throw new BusinessException(BizErrorCode.AUTH_PASSWORD_ERROR);
            }
        }
        return true;
    }


    /**
     * 根据房间id获取房间报名信息
     *
     * @param ilId 房间id
     * @return
     */
    @Override
    public ApplyEntityDTO getApply(String ilId) {
        LambdaQueryWrapper<ApplyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ApplyEntity::getIlId, Integer.parseInt(ilId));
        wrapper.orderByDesc(ApplyEntity::getCreatedAt);
        wrapper.last(" limit 1 ");
        ApplyEntity entity = limitApplyMapper.selectOne(wrapper);
        if (entity == null) {
            return null;
        }
        ApplyEntityDTO dto = new ApplyEntityDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public ApplyUserEntityDTO getApplyUserByPhoneAndIlId(Integer ilId, Long phone) {
        ApplyUserEntityDTO dto = new ApplyUserEntityDTO();
        LambdaQueryWrapper<ApplyUserEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ApplyUserEntity::getIlId, ilId);
        wrapper.eq(ApplyUserEntity::getPhone, phone);
        wrapper.last(" limit 1 ");
        ApplyUserEntity entity = applyUsersMapper.selectOne(wrapper);
        if (entity == null) {
            return null;
        }
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public WhiteAccountsEntityDTO getWhiteAccountsByNameAndIlId(Integer ilId, Long whitename) {
        WhiteAccountsEntityDTO dto = new WhiteAccountsEntityDTO();
        LambdaQueryWrapper<WhiteAccountsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WhiteAccountsEntity::getIlId, ilId);
        wrapper.eq(WhiteAccountsEntity::getWhitename, whitename);
        wrapper.last(" limit 1 ");
        WhiteAccountsEntity entity = whiteAccountsMapper.selectOne(wrapper);
        if (entity == null) {
            return null;
        }
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }
}
